
'''
author:zengtianqi
name:三乐购物
date:2021-7-19
function：能登录注册用户，能正常购买商品并且能准确计算购买后的余额
不足：充值金额等功能模块还未添加，商品库存问题等等等等等
'''
#商品列表
goods_list = {'F01':{'name':'苹果', 'price':1, 'amount':100},
              'F02':{'name':'香蕉', 'price':2, 'amount':100},
              'F03':{'name':'芒果', 'price':3, 'amount':100},
              'F04':{'name':'草莓', 'price':4, 'amount':100}
              }

#定义购物车
shopping_list = {}

#初始用户名
user_info = {'ztq':{'密码':'123', '余额':'10000'}}

#用户登录
def enter():

    #定义全局变量user_name,user_pwd
    global user_name ,user_pwd
    user_name = input("请输入用户名：")
    user_pwd = input("请输入密码：")

    #用户登录验证
    if user_name in user_info and user_pwd == user_info[user_name]['密码']:
        print("登陆成功")
        print("您的余额如下：", user_info[user_name]['余额'])
        print('''
        ---------------商品信息表----------------
              F01:name:苹果, price:1, amount:100
              F02:name:香蕉, price:2, amount:100
              F03:name:芒果, price:3, amount:100
              F04:name:草莓, price:4, amount:100
        ---------------------------------------
        ''')

        #调用shopping函数
        shopping()
        #  show_shoppinglist()
    
    else:
        print("用户不存在或者密码输入错误，请重新输入!")
        return enter()

#定义用户注册函数
def register():
    user_name = input("请输入要注册的用户名：")
    user_pwd = input("请输入密码：")
    moeny = input("请输入要充值的金额：")
    user_info.update({user_name:{'密码':user_pwd, '余额':moeny}})
    print("注册成功，请登录！")
    return sanle_shopping_menu()

#定义购物函数
def shopping():
  
  #定义全局变量花费总价格sum_price
  global sum_price
  
  #进入买东西循环，按q退出循环即结账
  while 1:

     print("购物愉快，按q结账!")
     goods_num = input("请选择你需要购买的商品编号：")

     if goods_num != 'q':
         amount = input("请输入采购的数量：")
         
         #初始化值花费总价格为0
         sum_price = 0

         #判断输入的数量是否是数字
         if amount.isdigit():
             amount = float(amount)
             
             #判断购物车中是否有重复的商品，有的话合并一起结账
             if goods_list[goods_num]['name'] in shopping_list.keys():
                 total_price = float(goods_list[goods_num]['price'] * amount)
                 shopping_list.update({goods_list[goods_num]['name']: {'价格': goods_list[goods_num]['price'],
                                                                       '数量':shopping_list[goods_list[goods_num]['name']]['数量'] + amount,
                                                                       '总价':shopping_list[goods_list[goods_num]['name']]['总价'] + total_price}})

             else:
                 total_price = float(goods_list[goods_num]['price'] * amount)
                 shopping_list.update({goods_list[goods_num]['name']: {'价格': goods_list[goods_num]['price'],
                                                                       '数量': amount,
                                                                       '总价': total_price}})
                #打印购物车
                 print("购物车：", shopping_list)
                 
            
                 for i in shopping_list.values():
                     sum_price = sum_price + i['总价']
                     print("需要支付：", sum_price, "元")
                     user_moeny = float(user_info[user_name]['余额'])
                     print("余额：", user_moeny, "元")
         else:
             print("输入数量有误请重新输入！")
             return shopping()

     else:
         user_moeny = float(user_info[user_name]['余额']) - sum_price
         print("结账成功！您的购物清单如下：")
         print(shopping_list)
         print("已支付：", sum_price)
         print("您的余额：", user_moeny)
         print("欢迎下次光临！".center(100, "-"))
         break



#菜单函数
def sanle_shopping_menu():
    print("欢迎来到三乐网上购物系统！".center(50, '-'))
    print("""
                     1.登录
                     2.注册
                     3.退出
        """)
    print("-"*59)
    choose = input("请选择你要进行的操作：")
    if choose == "1":
        return enter()
    elif choose == "2":
        return register()
    elif choose == "3":
        print("慢走嘞您！")
    else:
        print("输入错误，请重新输入！")
        return sanle_shopping_menu()
sanle_shopping_menu()










